package com.simple.util.base.extend;

/**
 * @program: simple_tools
 * @description:
 * @author: ChenWenLong
 * @create: 2019-06-04 14:06
 **/
public class WordUtil {

    public WordUtil() {
        super();
    }

    /**
     * 功能描述:
     * 〈将str中的空格替换成分割符',',wrapLength表示从str的最后一个字符往回数的索引位置〉
     *
     * @params : [str, wrapLength]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 14:10
     */
    public static String wrap(String str, int wrapLength) {
        return wrap(str, wrapLength, null, false);
    }

    /**
     * 功能描述:
     * 〈将newLineStr替换str中的空字符串,warpLength为倒数开始替换的索引位置,wrapLongWords为是否替换长字符串〉
     *
     * @params : [str, wrapLength, newLineStr, wrapLongWords]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 14:21
     */
    public static String wrap(String str, int wrapLength, String newLineStr, boolean wrapLongWords) {
        if (str == null) {
            return null;
        }
        if (newLineStr == null) {
            newLineStr = ",";
        }
        if (wrapLength < 1) {
            wrapLength = 1;
        }
        int inputLineLength = str.length();
        int offset = 0;
        StringBuffer wrappedLine = new StringBuffer(inputLineLength + 32);

        while ((inputLineLength - offset) > wrapLength) {
            if (str.charAt(offset) == ' ') {
                offset++;
                continue;
            }
            int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);

            if (spaceToWrapAt >= offset) {
                // normal case
                wrappedLine.append(str.substring(offset, spaceToWrapAt));
                wrappedLine.append(newLineStr);
                offset = spaceToWrapAt + 1;

            } else {
                // really long word or URL
                if (wrapLongWords) {
                    // wrap really long word one line at a time
                    wrappedLine.append(str.substring(offset, wrapLength + offset));
                    wrappedLine.append(newLineStr);
                    offset += wrapLength;
                } else {
                    // do not wrap really long word, just extend beyond limit
                    spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
                    if (spaceToWrapAt >= 0) {
                        wrappedLine.append(str.substring(offset, spaceToWrapAt));
                        wrappedLine.append(newLineStr);
                        offset = spaceToWrapAt + 1;
                    } else {
                        wrappedLine.append(str.substring(offset));
                        offset = inputLineLength;
                    }
                }
            }
        }

        // Whatever is left in line is short enough to just pass through
        wrappedLine.append(str.substring(offset));

        return wrappedLine.toString();
    }

    /**
     * 功能描述:
     * 〈首字母大写〉
     *
     * @params : [str]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:07
     */
    public static String capitalize(String str) {
        return capitalize(str, null);
    }

    /**
     * 功能描述:
     * 〈驼峰表示法,delimiters为字母连接符〉
     *
     * @params : [str, delimiters]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:08
     */
    public static String capitalize(String str, char[] delimiters) {
        int delimLen = (delimiters == null ? -1 : delimiters.length);
        if (str == null || str.length() == 0 || delimLen == 0) {
            return str;
        }
        int strLen = str.length();
        StringBuffer buffer = new StringBuffer(strLen);
        boolean capitalizeNext = true;
        for (int i = 0; i < strLen; i++) {
            char ch = str.charAt(i);
            if (isDelimiter(ch, delimiters)) {
                buffer.append(ch);
                capitalizeNext = true;
            } else if (capitalizeNext) {
                buffer.append(Character.toTitleCase(ch));
                capitalizeNext = false;
            } else {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }

    /**
     * 功能描述:
     * 〈首字母大写并且遵从驼峰表示法〉
     *
     * @params : [str]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:11
     */
    public static String capitalizeFully(String str) {
        return capitalizeFully(str, null);
    }

    /**
     * 功能描述:
     * 〈首字母大写并且遵从驼峰表示法,delimiters为空格替换的连接符〉
     *
     * @params : [str, delimiters]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:12
     */
    public static String capitalizeFully(String str, char[] delimiters) {
        int delimLen = (delimiters == null ? -1 : delimiters.length);
        if (str == null || str.length() == 0 || delimLen == 0) {
            return str;
        }
        str = str.toLowerCase();
        return capitalize(str, delimiters);
    }

    /**
     * 功能描述:
     * 〈首字母小写〉
     *
     * @params : [str]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:13
     */
    public static String uncapitalize(String str) {
        return uncapitalize(str, null);
    }

    /**
     * 功能描述:
     * 〈首字母小写,delimiters替换空格分隔符〉
     *
     * @params : [str, delimiters]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:13
     */
    public static String uncapitalize(String str, char[] delimiters) {
        int delimLen = (delimiters == null ? -1 : delimiters.length);
        if (str == null || str.length() == 0 || delimLen == 0) {
            return str;
        }
        int strLen = str.length();
        StringBuffer buffer = new StringBuffer(strLen);
        boolean uncapitalizeNext = true;
        for (int i = 0; i < strLen; i++) {
            char ch = str.charAt(i);

            if (isDelimiter(ch, delimiters)) {
                buffer.append(ch);
                uncapitalizeNext = true;
            } else if (uncapitalizeNext) {
                buffer.append(Character.toLowerCase(ch));
                uncapitalizeNext = false;
            } else {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }

    /**
     * 功能描述:
     * 〈将str中的大写换成小写,小写换成大写〉
     *
     * @params : [str]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:15
     */
    public static String swapCase(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return str;
        }
        StringBuffer buffer = new StringBuffer(strLen);

        boolean whitespace = true;
        char ch = 0;
        char tmp = 0;

        for (int i = 0; i < strLen; i++) {
            ch = str.charAt(i);
            if (Character.isUpperCase(ch)) {
                tmp = Character.toLowerCase(ch);
            } else if (Character.isTitleCase(ch)) {
                tmp = Character.toLowerCase(ch);
            } else if (Character.isLowerCase(ch)) {
                if (whitespace) {
                    tmp = Character.toTitleCase(ch);
                } else {
                    tmp = Character.toUpperCase(ch);
                }
            } else {
                tmp = ch;
            }
            buffer.append(tmp);
            whitespace = Character.isWhitespace(ch);
        }
        return buffer.toString();
    }

    /**
     * 功能描述:
     * 〈简写字符〉
     *
     * @params : [str]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:16
     */
    public static String initials(String str) {
        return initials(str, null);
    }

    /**
     * 功能描述:
     * 〈简写字符,delimiters为分隔符〉
     *
     * @params : [str, delimiters]
     * @return : java.lang.String
     * @author : cwl
     * @date : 2019/6/4 15:16
     */
    public static String initials(String str, char[] delimiters) {
        if (str == null || str.length() == 0) {
            return str;
        }
        if (delimiters != null && delimiters.length == 0) {
            return "";
        }
        int strLen = str.length();
        char[] buf = new char[strLen / 2 + 1];
        int count = 0;
        boolean lastWasGap = true;
        for (int i = 0; i < strLen; i++) {
            char ch = str.charAt(i);

            if (isDelimiter(ch, delimiters)) {
                lastWasGap = true;
            } else if (lastWasGap) {
                buf[count++] = ch;
                lastWasGap = false;
            } else {
                // ignore ch
            }
        }
        return new String(buf, 0, count);
    }

    /**
     * 功能描述:
     * 〈校验ch是否是分割符〉
     *
     * @params : [ch, delimiters]
     * @return : boolean
     * @author : cwl
     * @date : 2019/6/4 15:17
     */
    private static boolean isDelimiter(char ch, char[] delimiters) {
        if (delimiters == null) {
            return Character.isWhitespace(ch);
        }
        for (int i = 0, isize = delimiters.length; i < isize; i++) {
            if (ch == delimiters[i]) {
                return true;
            }
        }
        return false;
    }

}
